package com.bsit.obu.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


/**
 * ViewHolder
 */
public class ViewHolder {

	private SparseArray<View> array;// 保存布局中的控件-android提供的比HashMap效率更高的Map
	private View mConverView;// 布局
	private int mPosition;// 位置标识

	public ViewHolder(Context context, ViewGroup parent, int layoutId,
			int position) {
		array = new SparseArray<View>();
		mPosition = position;
		mConverView = LayoutInflater.from(context).inflate(layoutId, parent,
				false);
		mConverView.setTag(this);
	}

	/**
	 * 获取ViewHolder
	 * 
	 * @param context
	 * @param convertView
	 * @param parent
	 * @param layoutId
	 * @param position
	 * @return
	 */
	public static ViewHolder getViewHodler(Context context, View convertView,
			ViewGroup parent, int layoutId, int position) {
		if (convertView == null) {
			return new ViewHolder(context, parent, layoutId, position);
		} else {
			ViewHolder holder = (ViewHolder) convertView.getTag();
			holder.mPosition = position;
			return holder;
		}
	}

	public View getmConverView() {
		return mConverView;
	}

	/**
	 * 通过viewId获取控件
	 * 
	 * @param viewId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public <T extends View> T getView(int viewId) {
		View view = array.get(viewId);
		if (view == null) {
			view = mConverView.findViewById(viewId);
			array.put(viewId, view);
		}
		return (T) view;
	}

	
	/**
	 * @param viewId
	 * @return
	 */
	public ImageView getIv(int viewId) {
		ImageView iv = getView(viewId);
		return iv;
	}
	/**
	 * 设置文字
	 * 
	 * @param viewId
	 * @param text
	 * @return
	 */
	public ViewHolder setText(int viewId, String text) {
		TextView tv = getView(viewId);
		tv.setText(text);
		return this;
	}

	/**
	 * 设置文字
	 *
	 * @param viewId
	 * @param text
	 * @return
	 */
	public ViewHolder setText(int viewId, String text, int color) {
		TextView tv = getView(viewId);
		tv.setText(text);
		tv.setTextColor(color);
		return this;
	}
	/**
	 * @param viewId
	 * @return
	 */
	public ViewHolder setFlags(int viewId) {
		TextView tv = getView(viewId);
		tv.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
		return this;
	}
	
	/**
	 * @param viewId
	 * @return
	 */
	public ViewHolder setLayoutParams(int viewId, LinearLayout.LayoutParams lp) {
		View view = getView(viewId);
		view.setLayoutParams(lp);
		return this;
	}

	/**
	 * @param viewId
	 * @param bitmap
	 * @return
	 */
	public ViewHolder setImageBitmap(int viewId, Bitmap bitmap) {
		ImageView iv = getView(viewId);
		iv.setImageBitmap(bitmap);
		return this;
	}

	/**
	 * @param viewId
	 * @param resId
	 * @return
	 */
	public ViewHolder setImageResource(int viewId, int resId) {
		ImageView iv = getView(viewId);
		iv.setImageResource(resId);
		return this;
	}
	/**
	 * @param viewId
	 * @return
	 */
	public ViewHolder setImageVisble(int viewId, boolean isVisble) {
		ImageView iv = getView(viewId);
		if (isVisble){
			iv.setVisibility(View.VISIBLE);
		} else {
			iv.setVisibility(View.GONE);
		}
		return this;
	}

	/**
	 * @param viewId
	 * @param drawable
	 * @return
	 */
	public ViewHolder setImageDrawable(int viewId, Drawable drawable) {
		ImageView iv = getView(viewId);
		iv.setImageDrawable(drawable);
		return this;
	}



}
